package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.entity.vo.CourseQuery;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author LEO
 * @since 2021-07-19
 */
@RestController
@RequestMapping("/eduservice/edu-course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    private   EduCourseService  eduCourseService;

    //分页带条件查询课程
    @GetMapping("/getCourseList")
    public R getCourseList(){
        List<EduCourse> list = eduCourseService.list(null);
        return R.ok().data("list",list);
    }


    //添加课程信息
     @PostMapping("/addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
         //返回添加之后课程id，为了后面添加大纲使用
         String id = eduCourseService.saveCourseInfo(courseInfoVo);
         return R.ok().data("courseId",id);
     }


    //根据课程id查询课程信息
    @GetMapping("/getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
        CourseInfoVo courseInfoVo = eduCourseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo",courseInfoVo);
    }


    //修改课程信息
    @PostMapping("/updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo2){
         eduCourseService.updateCourseInfo(courseInfoVo2);
        return R.ok();
    }


    //根据id查询课程确认信息

    @GetMapping("/getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id){
        CoursePublishVo coursePublishVo= eduCourseService.publishCourseInfo(id);
        return R.ok().data("coursePublishVo",coursePublishVo);
    }


    //课程最终发布
    @PostMapping("/publicCourse/{id}")
    public R publicCourse(@PathVariable String id){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
         eduCourseService.updateById(eduCourse);
         return  R.ok();
    }

    //删除课程
    @DeleteMapping("/deleteCourse/{courseId}")
    public R deleteCourse(@PathVariable String courseId){
        eduCourseService.removeCourse(courseId);
        return R.ok();
    }


    //条件查询带分页
    @PostMapping("/pageCourseCondition/{current}/{limit}")
    public R pageCourseCondition(@ApiParam(name = "current",value = "当前页")@PathVariable long current,
                                 @ApiParam(name = "limit",value = "每页显示条数")@PathVariable long limit,
                                 @RequestBody(required = false) CourseQuery courseQuery){

        Page<EduCourse> page = new Page<>(current,limit);
        //构建条件
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        // 多条件组合查询
        String status = courseQuery.getStatus();
        String title = courseQuery.getTitle();
        //判断条件是否为空，如果不为空拼接条件
        if (!StringUtils.isEmpty(status)){
            queryWrapper.eq("status",status);
        }
        if (!StringUtils.isEmpty(title)){
            queryWrapper.like("title",title);
        }
        //排序
        queryWrapper.orderByDesc("gmt_create");
        //调用方法时候，底层封装，把分页所有数据封装到pageCourse对象里面
        eduCourseService.page(page,queryWrapper);
        long total = page.getTotal(); //总记录数
        List<EduCourse> records = page.getRecords(); //数据list集合
        return R.ok().data("total",total).data("rows",records);
    }
}

